<?php
 mb_internal_encoding("utf-8");
 //gc_enabled();
 require "../document/class_corpus.php";
 require "../document/class_document.php";
 require "../document/class_document_xml.php";

 require "../files/tool_files_dirs.php";

 require "../misc/class_tag.php";
 require "../misc/tool_color.php";

 require "../tree/class_tree.php";
 require "../tree/class_tree_xhtml.php";
 require "../tree/class_structure.php";
 require "../tree/tool_tree.php";
 require "../tree/class_drawer_svg.php";

 require "../array/tool_array.php";

 require "../pear_graph/tool_pear_graph.php";

 require "../maths/tool_maths.php";

 require "../string/tool_string.php";
 require "../string/class_segment.php";

 require "./tool_alignement.php";

$dir = "./corpus/test/";
$dir = "./corpus/europa_unco/";
$lg_candidates=array("cs","da","de","el","en","es","et","fi","fr","hu","it","lt","lv","nl","pl","pt","sl","sw");
$array_ids_europa=tool_corpus::get_unified_europa($dir,$lg_candidates);

//print_r($array_ids_europa);
//die();

//$motif_lg="fr";
//$array_ids_europa=tool_corpus::europa_get_all_ids($dir);
$array_corpus_groupe=array();
//$start=microtime(true);
$cpt = $taille = 0;
$array_bloc_str = $array_clique = array();

foreach($array_ids_europa as $id_europa){
  $array_doc = array();
  foreach($lg_candidates as $lg){
    $corpus = tool_corpus::get_corpus_regexp($dir,$id_europa."$lg");
    $ad = $corpus->__get_array_doc();
    foreach($ad as $d) $array_doc[]=$d;
  }
  $array_clique[$id_europa] = array();
  foreach($array_doc as $document){
    $id_doc = mb_strcut($document->__get_id(),-6,2);
    $structure = $document->__get_structure();
    $signature = $structure->__get_signature();
    $signature->__recalc_idn();
    $flag = false;
    foreach($array_clique[$id_europa] as $id_clique=>$array_sign){
      $sign = reset($array_sign);
      if($sign->__is_same($signature)){
        $array_clique[$id_europa][$id_clique][$id_doc] = $signature;
        $flag = true;
        break;
      }
    }
    if(!$flag)
      $array_clique[$id_europa][] = array($id_doc=>$signature);
  }
}

/*
$cpt_nb_class = array();
$cpt_type_class = array();
foreach($array_clique as $id_multidoc=>$clique){
  echo $id_multidoc." ".count($clique)."\n";
  $cpt_nb_class[count($clique)]++;
  $array_mot = array();
  foreach($clique as $id_clique=>$array_doc){
    $array_mot[] = count($array_doc);
    echo "  ".count($array_doc)."\n";
  }
  sort($array_mot);
  $mot = implode(":",$array_mot);
  $cpt_type_class[$mot]++;
}

print_r($cpt_nb_class);
print_r($cpt_type_class);

die();
*/

foreach($array_clique as $id_multidoc=>$clique){
  if(count($clique)>1){
    $max_x = $max_y =0;
    $array_repartition = $array_pente = array();
    foreach($clique as $id_clique=>$array_struct){
      $first_struct = reset($array_struct);
      $rep = tool_tree::structure2strlen($first_struct);
      $rep = array_slice($rep,1);
      $array_repartition[$id_clique] = $rep;
      $count = count(tool_maths::get_array_pente($rep));
      $max_x = max($max_x,$count);
      $max_y = max($max_y,max($rep));
    }
    $out.="<h2>$id_multidoc</h2>";

    $id_clique_src = array_pop(array_keys($clique));
    $array_struct_src = array_pop($clique);
    $s0 = reset($array_struct_src);
    $first_struct_src = tool_tree::structure2struct_idn($s0);
    $rep_src = $array_repartition[$id_clique_src];
    $array_keys_src = array_keys($array_clique[$id_multidoc][$id_clique_src]);
    $first_name_src = current($array_keys_src);
    $title_src = "[$id_clique_src] : ".tool_array::printr($array_keys_src);

    foreach($clique as $id_clique=>$array_struct){
      $s1 = reset($array_struct);
      $first_struct = tool_tree::structure2struct_idn($s1);
      $rep = $array_repartition[$id_clique];
      $array_keys = array_keys($array_clique[$id_multidoc][$id_clique]);
      $first_name = current($array_keys);
      $res=tool_alignement::equiv2graph($rep,$rep_src,$max_x+1,$max_y);

      $rev0 = $res["rev_equiv0"];
      $rev1 = $res["rev_equiv1"];
      $option0 = array("print"=>"color_group","color_group"=>$rev0);
      $option1 = array("print"=>"color_group","color_group"=>$rev1);
      $svg0 = tool_tree::tree_group_numeric2svg($first_struct,$option0);
      $svg1 = tool_tree::tree_group_numeric2svg($first_struct_src,$option1);

      $graph_equiv=$res["graph"];
      $out.="
        <div>
        <table border=\"1\">
        <tr>
          <td valign=\"middle\">
            $first_name
            <hr/>
            $first_name_src
          </td>
          <td valign=\"middle\">$graph_equiv</td>
          <td valign=\"middle\">
            $svg0
            <hr/>
            $svg1
          </td>
        </tr>
        </table>
        </div>
      ";
    }
  }
}


$out="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
                      \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\"
      xmlns:svg=\"http://www.w3.org/2000/svg\">
$out
</html>
";

echo $out;

die();
?>
